import { defineStore } from "pinia";
import { ref } from "vue"
import { loginApi } from "@/api/login";
import { localCache } from "@/utils/cache";
import { mapMenusToRoutes } from "@/utils/mapMenus";
import router from "@/router";
import { TOKEN } from '@/global/constans'

interface UserInfo {
  userId: string;
  userName: string;
  token: string;
}

export const useUserStore = defineStore("user", () => {
  let userInfo = ref({
    userId: "",
    userName: localCache.getCache("username") || "",
    token: localCache.getCache(TOKEN) || "",
    userInfo: {},
  })
  // 暂时死数据替代
  const userMenu: any = ref(
    [
      {
        "id": "2",
        "path": "/SystemManagement",
        "component": "",
        "alwaysShow": true,
        "name": "系统管理",
        "meta": {
          "title": "系统管理",
          "icon": "Setting",
          "roles": [2, 1]
        },
        "children": [{
          "id": "2-1",
          "path": "/SystemManagement/UserManagement",
          "component": "UserManagement",
          "alwaysShow": false,
          "name": "用户管理",
          "meta": {
            "title": "用户管理",
            "icon": "Setting",
            "roles": [1]
          }
        }, {
          "id": "2-2",
          "path": "/SystemManagement/RoleManagement",
          "component": "RoleManagement",
          "alwaysShow": false,
          "name": "角色管理",
          "meta": {
            "title": "角色管理",
            "icon": "peoples",
            "roles": [2, 1]
          }
        }, {
          "id": "2-3",
          "path": "/SystemManagement/DepartmentManagement",
          "component": "DepartmentManagement",
          "alwaysShow": false,
          "name": "部门管理",
          "meta": {
            "title": "部门管理",
            "icon": "tree",
            "roles": [1, 2]
          }
        }, {
          "id": "2-4",
          "path": "/SystemManagement/MenuManagement",
          "component": "MenuManagement",
          "alwaysShow": false,
          "name": "菜单管理",
          "meta": {
            "title": "菜单管理",
            "icon": "tree-table",
            "roles": [1, 2]
          }
        }]
      },
      {
        "id": "4",
        "path": "/SystemMonitoring",
        "component": "",
        "alwaysShow": true,
        "name": "系统监控",
        "meta": {
          "title": "系统监控",
          "icon": "DataLine",
          "roles": [2, 1]
        },
        "children": [{
          "id": "4-1",
          "path": "/SystemMonitoring/OnlineUsers",
          "component": "OnlineUsers",
          "alwaysShow": false,
          "name": "在线用户",
          "meta": {
            "title": "在线用户",
            "icon": "user",
            "roles": [1]
          }
        }]
      }
    ]
  )
  const login = async (params: any) => {
    const res = (await loginApi(params)) as unknown as UserInfo
    userInfo.value.userId = res.userId
    userInfo.value.userName = res.userName
    userInfo.value.token = res.token
    localCache.setCache(TOKEN, userInfo.value.token)
    // TODO 获取用户详细信息
    // TODO 获取用户权限
    // 获取用户菜单
    const dynamicRoutes = mapMenusToRoutes(userMenu.value)
    dynamicRoutes.forEach(item => {
      router.addRoute("layout", item)
    })
    router.push("/layout")
  }
  const refreshMenu = () => {
    const dynamicRoutes = mapMenusToRoutes(userMenu.value)
    dynamicRoutes.forEach(item => {
      router.addRoute("layout", item)
    })
  }
  return {
    userInfo,
    userMenu,
    login,
    refreshMenu
  }
})
